<template>
  <div>
    <div>
      <el-button type="success" size='small' @click="dialogVisible()" class="addClick">{{$t('添加')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
    </div>
    <el-table size='mini' ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55">
      </el-table-column>
      <el-table-column :label="$t('序号')" align='center' min-width="300">
        <template slot-scope="scope">{{ 'DNS' + (scope.$index + 1) }}</template>
      </el-table-column>
      <el-table-column :label="$t('地址')" align='center' min-width="300">
        <template slot-scope="scope">{{ scope.row }}</template>
      </el-table-column>
    </el-table>
    <dns-dialog v-if="dnsDialogVisible" ref="refDnsDialogVisible"></dns-dialog>
  </div>
</template>
<script>
import { getDnsArray, delDnsInfo } from '@/api/network/dnsHosts/dns'
import DnsDialog from './components/DnsDialog'
export default {
  name: 'Dns',
  components: {
    DnsDialog
  },
  data () {
    return {
      dnsDialogVisible: false,
      tableData: [],
      addrStr: ''
    }
  },
  created () {
    this.listData()
  },
  methods: {
    // 多选按钮
    handleSelectionChange (data) {
      var addrStr = ''
      for (var item of data) {
        if (addrStr === '') {
          addrStr = item
        } else {
          addrStr = addrStr + '@#@' + item
        }
      }
      this.addrStr = addrStr
    },
    // 列表
    listData () {
      getDnsArray().then(res => {
        if (res.status === 0) {
          this.tableData = res.info[0].dns
        }
      })
    },
    // 添加按钮表单
    dialogVisible (data) {
      this.dnsDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refDnsDialogVisible.init(data)
      })
    },
    deleteData () {
      if (this.addrStr) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delDnsInfo({ addr: this.addrStr }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.addrStr = ''
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    }
  }
}
</script>
